<?php

/* Database Configuration. Add your details below */
ini_set('display_errors', 'On');

/* Database Config End */

error_reporting(E_ALL ^ E_NOTICE);

require_once "classes/DB.class.php";
require_once "classes/Chat.class.php";
require_once "classes/User.class.php";
require_once "classes/Nebula.class.php";
require_once "classes/Post.class.php";
require_once "classes/Galaxy.class.php";
require_once "dbsetting.php";

session_start();

DB::init($dbOptions);

if (isset($_SESSION['user'])) {
    User::update_online_ts();
}

if (isset($_GET['action'])) {
    $log = array();

    switch ($_GET['action']) {
        case('submitChat'):
            $log = Chat::submitChat($_POST['gid'], $_POST['text']);
            break;
        case('getChats'):
            $log = Chat::getChats($_GET['gid'], $_GET['lastID'], $_GET['max_num']);
            break;
        case('getLatestChats'):
            $log = Chat::getLatestChats($_GET['gid'], $_GET['max_num']);
            break;
        case('getAllChats'):
            $log = Chat::getAllChats();
            break;
        case('getChatsLog'):
            $log = Chat::getChatsLog($_GET['gid']);
            break;
        case('blockUser'):
            $log = Chat::blockUser($_GET['uid_to']);
            break;
        case('unblockUser'):
            $log = Chat::unblockUser($_GET['uid_to']);
            break;

        //user login facilities
        case('login'):
            $log = User::login($_POST);
            break;
        case('logout'):
            // do something with the connected db.
            $log = User::logout();
            header('Location: ./login.php');
            break;
        case('register'):
            $log = User::register($_POST);
            break;
        case('activate'):
            $log = User::activate($_GET['uid'], $_GET['code']);
            break;
        case('generateInvites'):
            $log = User::generateInvite($_GET['from'], $_GET['num']);
            break;
        case('getInvites'):
            $log = User::getInvites();
            break;
        case('circle'):
            $log = User::circle($_GET['uid_to'], $_GET['nid']);
            break;
        case("uncircle"):
            $log = User::uncircle($_GET['uid_to'], $_GET['nid']);
            break;
        case("uncircle_all_nebulas"):
            $log = User::uncircle_all_nebulas($_GET['uid_to']);
            break;
        case("getCircles"):
            $log = User::getCircles($_GET['uid']);
            break;
        case('checkpsw'):
            $log = User::checkpsw($_POST);
            break;
        case('checkfield'):
            $log = User::checkfield($_GET);
            break;
        case('updatefield'):
            $log = User::updatefield($_POST);
            break;
        case('update_settings'):
            $log = User::update_settings($_GET);
            break;
        case('getUser'):
            $log = User::getUser($_GET['uid']);
            break;
        case('getSelfInfo'):
            $log = User::getSelfInfo();
            break;
        case('all_users'):
            $log = User::all_users();
            break;
        case('online_users'):
            $log = User::all_online_users();
            break;
        case('update_online_ts'):
            $log = User::update_online_ts();
            break;

        case('all_nebulas'):
            $log = Nebula::nebulas($_GET);
            break;
        case('all_nebulasv2'):
            $log = Nebula::all_nebulas();
            break;
        case('get_nebula_name'):
            $log = Nebula::get_nebula_name($_GET['nid']);
            break;
        case('top_nebulas'):
            $log = Nebula::get_top_nebulas($_GET['n']);
            break;
        case('nebulas_for_uid'):
            $log = Nebula::nebulas_for_uid($_GET['uid']);
            break;
        case('get_common_nebulas'):
            $log = Nebula::get_common_nebulas($_GET['uid_to']);
            break;
        case('add_nebula'):
            $log = Nebula::add_nebula($_GET);
            break;
        case('tag_nebula'):
            $log = Nebula::tag_nebula($_GET['tags']);
            break;
        case('untag_nebula'):
            $log = Nebula::untag_nebula($_GET['nid']);
            break;
        case('get_nebula_content'):
            $log = Nebula::get_nebula_content($_GET['nid']);
            break;

        case('new_post'):
            $log = Post::new_post($_POST);
            break;
        case('update_post'):
            $log = Post::update_post($_POST);
            break;
        case('delete_post'):
            $log = Post::delete_post($_GET['pid']);
            break;
        case('rate_post'):
            $log = Post::rate_post($_GET['pid'], $_GET['rate']);
            break;
        case('rated_by_uid'):
            $log = Post::rated_by_uid($_GET['pid'], $_GET['uid']);
            break;
        case('show_posts'):
            $log = Post::show_posts($_GET['max_num']);
            break;
        case('total_score_post'):
            $log = Post::total_score($_GET['pid']);
            break;
        case('top_posts'):
            $log = Post::show_posts($_GET['n']);
            break;
        case('recent_posts'):
            $log = Post::recent_posts($_GET['max_num'], $_GET['uid']);
            break;
        case('mark_post'):
            $log = Post::mark_post($_POST['pid']);
            break;
        case('unmark_post'):
            $log = Post::unmark_post($_POST['pid']);
            break;
        case('get_post_content'):
            $log = Post::get_post_content($_GET['pid']);
            break;

        case('get_post_galaxies'):
            $log = Post::get_post_galaxies($_GET['pid']);
            break;


        case('galaxy_users'):
            $log = Galaxy::galaxy_users($_GET['gid']);
            break;
        case('add_galaxy'):
            $log = Galaxy::add_galaxy($_POST);
            break;
        case('update_galaxy'):
            $log = Galaxy::update_galaxy($_POST);
            break;
        case('visit_galaxy'):
            $log = Galaxy::visit_galaxy($_GET['gid']);
            break;
        case('delete_galaxy'):
            $log = Post::delete_galaxy($_GET['gid']);
            break;
        case('total_score_galaxy'):
            $log = Galaxy::total_score($_GET['gid']);
            break;
        case('rated_by_uid'):
            $log = Galaxy::rated_by_uid($_GET['gid'], $_GET['uid']);
            break;
        case('rate_galaxy'):
            $log = Galaxy::rate_galaxy($_GET['gid'], $_GET['rate']);
            break;
        case('tag_galaxy'):
            $log = Galaxy::tag_galaxy($_POST);
            break;
        case('all_galaxies'):
            $log = Galaxy::all_galaxies();
            break;
        case('galaxies_for_uid'):
            $log = Galaxy::galaxies_for_uid($_GET['uid']);
            break;
        case('galaxies_for_user_profile'): // sorted by join_ts
            $log = Galaxy::galaxies_for_user_profile($_GET['uid']);
            break;
        case('join_galaxy'):
            $log = Galaxy::join_galaxy($_GET['gid']);
            break;
        case('quit_galaxy'):
            $log = Galaxy::quit_galaxy($_GET['gid']);
            break;
        case('recent_galaxies'):
            $log = Galaxy::recent_galaxies($_GET['max_num'], $_GET['uid']);
            break;
        case('galaxies_namecard'):
            $log = Galaxy::galaxies_namecard_info($_GET['uid'], $_GET['nid']);
            break;
        default:
    }

    echo json_encode($log);
} else {
    
}
?>